//
//  BuyWindowView.m
//  StandardizedProduct
//
//  Created by 宁宁 on 16/6/29.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "BuyWindowView.h"

#import "BuyWindowStandardView.h"

@implementation BuyWindowView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setNet];
    }
    return self;
}

- (void)layoutSubviews{
    [self setView];
}
//网络请求
- (void)setNet{
    
}

- (void)setView{
    //隐藏购买窗
    _hiddenWindow = [UIButton buttonWithType:UIButtonTypeCustom];
    _hiddenWindow.frame = CGRectMake(0, 20, WIDTH, HEIGHT - 20);
    _hiddenWindow.backgroundColor = [UIColor blackColor];
    _hiddenWindow.alpha = 0.3;
    [_hiddenWindow addTarget:self action:@selector(hiddenWindow:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_hiddenWindow];
    
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    view.backgroundColor = [UIColor whiteColor];
    [self addSubview:view];
    
    //商品名称
    _goodsName = [[UILabel alloc] initWithFrame:CGRectMake(125, 15, 34, 21)];
    [view addSubview:_goodsName];
    
    //商品简介
    _goodsDetail = [[UILabel alloc] initWithFrame:CGRectMake(174, 20, WIDTH - 174, 16)];
    _goodsDetail.font = [UIFont systemFontOfSize:13];
    [view addSubview:_goodsDetail];
    
    //商品价格
    _price = [[UILabel alloc] initWithFrame:CGRectMake(128, 46, WIDTH - 128, 21)];
    _price.textColor = APPCOLOR;
    [view addSubview:_price];
    
    //规格
    CGFloat yFrame = 90;
    _chooseStandardArray = [NSMutableArray array];
    for (int i = 0; i < _standard.count; i++) {
        BuyWindowStandardView *standardView = [[BuyWindowStandardView alloc] initWithFrame:CGRectMake(0, yFrame, WIDTH, 100)];
        standardView.standardDic = _standard[i];
        [standardView setNeedsLayout];
        standardView.frame = CGRectMake(0, yFrame, WIDTH, standardView.height);
        
        NSDictionary *dic = _standard[i];
        NSString *key = dic.allKeys[0];
        NSArray *arr = [NSArray arrayWithObjects:key, nil];
        [_chooseStandardArray addObject:arr];
        
        [view addSubview:standardView];
        
        standardView.chooseStandardString = ^(NSString *string, NSString *standardString){
            [_chooseStandardArray replaceObjectAtIndex:i withObject:@[standardString, string]];
        };
        
        yFrame = yFrame + standardView.frame.size.height;
    }

    
    //数量
    UILabel *number = [[UILabel alloc] initWithFrame:CGRectMake(15, yFrame, 40, 25)];
    number.textColor = [UIColor grayColor];
    number.font = [UIFont systemFontOfSize:16];
    number.text = @"数量";
    [view addSubview:number];
    
    UIImageView *numImageView = [[UIImageView alloc] initWithFrame:CGRectMake(65, yFrame, 90, 25)];
    numImageView.image = [UIImage imageNamed:@"detailBuyicon_13"];
    [view addSubview:numImageView];
    
    // -
    _minusNum = [UIButton buttonWithType:UIButtonTypeCustom];
    _minusNum.frame = CGRectMake(66, yFrame + 1, 25, 23);
    _minusNum.backgroundColor = [UIColor whiteColor];
    [_minusNum setTitle:@"-" forState:UIControlStateNormal];
    [_minusNum setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_minusNum addTarget:self action:@selector(numMinus) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:_minusNum];
    
    // +
    _plusNum = [UIButton buttonWithType:UIButtonTypeCustom];
    _plusNum.frame = CGRectMake(129, yFrame + 1, 25, 23);
    _plusNum.backgroundColor = [UIColor whiteColor];
    [_plusNum setTitle:@"+" forState:UIControlStateNormal];
    [_plusNum setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_plusNum addTarget:self action:@selector(numPlus) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:_plusNum];
    
    //数值
    _num = [[UITextField alloc] initWithFrame:CGRectMake(92, yFrame + 1, 36, 23)];
    _num.text = @"1";
    _num.textColor = [UIColor grayColor];
    _num.textAlignment = NSTextAlignmentCenter;
    _num.keyboardType = UIKeyboardTypeNumberPad;
    _num.backgroundColor = [UIColor whiteColor];
    _num.clearsOnBeginEditing = YES;
    [view addSubview:_num];
    
    //分割线
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15, yFrame + 45, WIDTH - 30, 1)];
    line.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:line];
    
    //立即购买
    _buyRightNow = [UIButton buttonWithType:UIButtonTypeCustom];
    [_buyRightNow setImage:[UIImage imageNamed:@"detailBuyicon_17"] forState:UIControlStateNormal];
    _buyRightNow.frame = CGRectMake(WIDTH / 2 - 65, yFrame + 45 + 16, 130, 40);
    [_buyRightNow addTarget:self action:@selector(buyRightNow:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:_buyRightNow];
    
    UILabel *buyLabel = [[UILabel alloc] initWithFrame:_buyRightNow.frame];
    buyLabel.backgroundColor = [UIColor clearColor];
    buyLabel.text = @"立即购买";
    buyLabel.textColor = [UIColor whiteColor];
    buyLabel.font = [UIFont systemFontOfSize:16];
    buyLabel.textAlignment = NSTextAlignmentCenter;
    [view addSubview:buyLabel];
    
    
    //更新View的frame
    view.frame = CGRectMake(0, HEIGHT - (yFrame + 116), WIDTH, yFrame + 116);
    
    
    //商品图片
    _goodsImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, view.frame.origin.y - 35, 100, 100)];
    [self addSubview:_goodsImage];
    
    
    _goodsImage.backgroundColor = [UIColor yellowColor];
    _goodsName.text = @"零食";
    _goodsDetail.text = @"这是商品简介";
    _price.text = [NSString stringWithFormat:@"¥ 10000"];
}
//数量-
- (void)numMinus{
    if ([_num.text isEqualToString:@"1"]) {
        return;
    }
    _num.text = [NSString stringWithFormat:@"%d", [_num.text intValue] - 1];
}
//数量+
- (void)numPlus{
    _num.text = [NSString stringWithFormat:@"%d", [_num.text intValue] + 1];
}

//隐藏购买窗
- (void)hiddenWindow:(UIButton *)sender{
//    [UIView animateWithDuration:0.4 animations:^{
        self.frame = CGRectMake(0, HEIGHT, WIDTH, HEIGHT);
//    }];
}
//立即购买
- (void)buyRightNow:(UIButton *)sender{
    NSString *string = @"0";
    NSString *standardString;
    

    for (NSArray *arr in _chooseStandardArray) {
        //未选择规格 弹出警示框
        if (arr.count == 1) {
            string = @"1";
            standardString = arr[0];
            self.clickBuyRightNow(string, standardString);
            return;
        }
    }
    self.clickBuyRightNow(string, nil);
}

@end
